package com.rxnetwork.sample.module.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.rxnetwork.sample.R;
import com.rxnetwork.sample.model.ListModel;
import com.rxnetwork.transferee.TransfereeUtil;

import java.util.List;

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainAdapterHolder> {

    private MyItemClickListener mItemClickListener;

    private Context mContext;

    private List<ListModel> listModels = null;

    public MainAdapter(Activity activity, List<ListModel> listModels) {
        this.listModels = listModels;
        mContext = activity;
    }


    @Override
    public MainAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainAdapterHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false), mItemClickListener);
    }

    @Override
    public void onBindViewHolder(final MainAdapterHolder holder, final int position) {
        if (listModels == null) {
            return;
        }
        Glide
                .with(holder.imageView.getContext())
                .load(listModels.get(position).getTitleImage())
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher)
                .centerCrop()
                .into(holder.imageView);
        holder.textView.setText(listModels.get(position).getTitle());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.mListener != null) {
                    TransfereeUtil.getInstance().onClickShow(holder.imageView, listModels.get(position).getTitleImage());
                    holder.mListener.onItemClick(holder.imageView, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listModels == null ? 0 : listModels.size();
    }

    public void addAll(List<ListModel> data) {
        if (listModels != null) {
            listModels.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        if (listModels != null) {
            listModels.clear();
            notifyDataSetChanged();
        }
    }

    class MainAdapterHolder extends RecyclerView.ViewHolder{

        private AppCompatImageView imageView;
        private AppCompatTextView textView;
        private MyItemClickListener mListener;

        MainAdapterHolder(View itemView, MyItemClickListener listener) {
            super(itemView);
            imageView = (AppCompatImageView) itemView.findViewById(R.id.list_image);
            textView = (AppCompatTextView) itemView.findViewById(R.id.list_tv);

            mListener = listener;
        }
    }

    public interface MyItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }
}
